package first.group.ssh.packet;

import first.group.ssh.SSHConsts;
import first.group.ssh.packet.reader.IntegerReader;
import first.group.ssh.packet.reader.StringReader;
import first.group.ssh.packet.writer.StringWriter;

import java.io.IOException;
import java.nio.ByteBuffer;

public class DisconnectPacket extends Packet {
    public final int reason;
    public final String description;
    public final String language;

    public DisconnectPacket(int reason, String description, String language) {
        super(SSHConsts.SSH_MSG_DISCONNECT);
        this.reason = reason;
        this.description = description;
        this.language = language;
    }

    public static PacketParser<DisconnectPacket> PARSER = new DisconnectPacket.Parser();

    private static class Parser extends PacketParser<DisconnectPacket> {

        @Override
        public DisconnectPacket parse(ByteBuffer buffer) {
            var reason = IntegerReader.readInteger(buffer);
            if (reason == null) {
                return null;
            }
            var description = StringReader.readString(buffer);
            if (description == null) {
                return null;
            }
            var language = StringReader.readString(buffer);
            if (language == null) {
                return null;
            }
            return new DisconnectPacket(reason, description, language);
        }
    }

    @Override
    public void write(ByteBuffer buffer) {
        buffer.putInt(reason);
        StringWriter.writeString(buffer, description);
        StringWriter.writeString(buffer, language);
    }
}
